package com.xmall.utils;

import com.xmall.define.dict.OperatePlatformEnum;
import com.xmall.define.dto.login.MemberLoginInfo;
import com.xmall.define.dto.login.SysLoginInfo;
import com.xmall.security.MemberPhonePasswordAuthenticationToken;
import com.xmall.security.MemberPhoneVerifyAuthenticationToken;
import com.xmall.security.SysPhonePasswordAuthenticationToken;
import com.xmall.security.SysPhoneVerifyAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {

    /**
     * 获取操作人ID
     */
    public static String getOperateId() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if(null == authentication) {
                return "";
            }

            if(authentication instanceof SysPhonePasswordAuthenticationToken ||
                    authentication instanceof SysPhoneVerifyAuthenticationToken) {
                SysLoginInfo loginInfo = (SysLoginInfo) authentication.getDetails();
                return loginInfo.getUserId().toString();
            }

            if(authentication instanceof MemberPhonePasswordAuthenticationToken ||
                    authentication instanceof MemberPhoneVerifyAuthenticationToken) {
                MemberLoginInfo loginInfo = (MemberLoginInfo) authentication.getDetails();
                return loginInfo.getUserId().toString();
            }
        }catch (Exception e) {

        }

        return "";
    }

    /**
     * 获取操作人
     */
    public static String getOperateName() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if(null == authentication) {
                return "";
            }

            if(authentication instanceof SysPhonePasswordAuthenticationToken ||
                    authentication instanceof SysPhoneVerifyAuthenticationToken) {
                SysLoginInfo loginInfo = (SysLoginInfo) authentication.getDetails();
                return loginInfo.getNickname();
            }

            if(authentication instanceof MemberPhonePasswordAuthenticationToken ||
                    authentication instanceof MemberPhoneVerifyAuthenticationToken) {
                MemberLoginInfo loginInfo = (MemberLoginInfo) authentication.getDetails();
                return loginInfo.getNickname();
            }
        }catch (Exception e) {

        }

        return "";
    }

    /**
     * 获取操作人账号
     */
    public static String getOperateAccount() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if(null == authentication) {
                return "";
            }

            if(authentication instanceof SysPhonePasswordAuthenticationToken ||
                    authentication instanceof SysPhoneVerifyAuthenticationToken) {
                SysLoginInfo loginInfo = (SysLoginInfo) authentication.getDetails();
                return loginInfo.getAccount();
            }

            if(authentication instanceof MemberPhonePasswordAuthenticationToken ||
                    authentication instanceof MemberPhoneVerifyAuthenticationToken) {
                MemberLoginInfo loginInfo = (MemberLoginInfo) authentication.getDetails();
                return loginInfo.getAccount();
            }
        }catch (Exception e) {

        }

        return "";
    }

    /**
     * 获取操作平台
     */
    public static Integer getOperatePlatform() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if(null == authentication) {
                return null;
            }

            if(authentication instanceof SysPhonePasswordAuthenticationToken ||
                    authentication instanceof SysPhoneVerifyAuthenticationToken) {
                return OperatePlatformEnum.ADMIN_SYSTEM.getType();
            }

            if(authentication instanceof MemberPhonePasswordAuthenticationToken ||
                    authentication instanceof MemberPhoneVerifyAuthenticationToken) {
                return OperatePlatformEnum.SHOP_SYSTEM.getType();
            }
        }catch (Exception e) {

        }

        return null;
    }
}
